package com.jin.webvideo.base.dc;

import com.jin.webvideo.base.StringKeyValuePairs;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;

/**
 * @author biao532
 * @date 2023/12/03 18:05
 **/
public class KeyValuePairReader extends BufferedReader {
    private static final String PREFIX_COMMENT = "#";

    public KeyValuePairReader(Reader in, int sz) {
        super(in, sz);
    }

    public KeyValuePairReader(Reader in) {
        super(in);
    }

    @Override
    public String readLine() throws IOException {
        String line = null;
        while ((line = super.readLine()) != null) {
            int idx = line.indexOf(PREFIX_COMMENT);
            if (idx != -1) {
                line = line.substring(0, idx);
            }
            line = line.trim();
            if (!line.isEmpty()) {
                break;
            }
        }
        return line;
    }

    public StringKeyValuePairs readKV() throws IOException {
        String line = readLine();
        if (line == null) {
            return null;
        }

        if (line.indexOf(" ") != -1 || line.indexOf("\t") != -1) {
            throw new IOException("出现禁止的空白符");
        }
        return StringKeyValuePairs.createKeyValuePairs(line, "=");
    }
}
